﻿using System.Text.Json.Serialization;

namespace star_questionnair.Models;

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

/**
 * 用户实体
 */
[Table("user")]
public class User
{
    [Key]
    public int Id { get; set; } // 主键

    [Required(ErrorMessage = "用户名不能为空")]
    // [StringLength(50, ErrorMessage = "用户名长度不能超过50个字符")]
    [Column("user_name")]
    public string UserName { get; set; }

    [Required(ErrorMessage = "密码不能为空")]
    // [StringLength(20, MinimumLength = 6, ErrorMessage = "密码长度在6到20个字符之间")]
    public string Password { get; set; }

    // [Required(ErrorMessage = "邮箱为空")]
    // [EmailAddress(ErrorMessage = "邮箱格式错误")]
    public string Email { get; set; }

    // [Required(ErrorMessage = "性别为空")]
    public bool Gender { get; set; }

    // [Required(ErrorMessage = "无效公司")]
    public string? Enterprise { get; set; }

    // [StringLength(100, ErrorMessage = "个性签名长度不能超过100个字符")]
    public string? Signature { get; set; }

    [DataType(DataType.Date)]
    // [PastDate(ErrorMessage = "生日不能是未来的日期")]
    public DateTime? Birthday { get; set; }

    [Column("role_name")]
    public string RoleName { get; set; } = "默认";

    [Column("registered_time")] // 映射为数据库中的列名
    public DateTime RegisteredTime { get; set; }

    [Column("updated_time")] // 映射为数据库中的列名
    public DateTime UpdatedTime { get; set; }

    [NotMapped]
    [JsonIgnore]
    public string salt;//暂时没有添加salt的逻辑，由于userName更改的可能性，所以需要salt.
}

// 自定义验证特性，确保日期是过去的日期
public class PastDateAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        return value is DateTime date && date <= DateTime.Now;
    }
}
